<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
    <!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
    <meta name="renderer" content="webkit">
    <!--国产浏览器高速模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>穷在闹市出品</title>

    <!-- 公共样式 开始 -->
    <link rel="stylesheet" type="text/css" href="../../css/base.css">
    <link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
    <script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
    <script type="text/javascript" src="../../layui/layui.js"></script>
    <!-- 滚动条插件 -->
    <link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
    <script src="../../framework/jquery-ui-1.10.4.min.js"></script>
    <script src="../../framework/jquery.mousewheel.min.js"></script>
    <script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
    <script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
    <!-- 公共样式 结束 -->
    <script type="text/javascript" src="../../framework/frameAll.js"></script>

    <style>
        .layui-form {
            margin-right: 30%;
        }

        .layui-form-label {
            width: 100px;
        }

        .layui-input-block {
            margin-left: 130px;
        }
    </style>
</head>

<body>
<div class="cBody">
    <form id="addForm" class="layui-form" lay-filter="test">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" required lay-verify="required" placeholder="请输入标题"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">留言人</label>
            <div class="layui-input-inline shortInput">
                <input type="text" name="studentName" autocomplete="off" class="layui-input" readOnly="true">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">文本编辑器</label>
            <div class="layui-input-block">
                <textarea id="content" name="content" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitBut">立即提交</button>
                <button class="layui-btn layui-btn-primary" type="button" id="reset">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layedit'], function () {
        var form = layui.form;
        var layedit = layui.layedit;
        var index;

        $('#reset').click(function () {
            $('input[name=title]').val('');
            layedit.setContent(index, '');
        });

        //建立编辑器
        index = layedit.build('content', {
            tool: [
                'strong', //加粗 'italic' //斜体
                'underline', //下划线
                'del', //删除线
                '|', //分割线
                'left', //左对齐
                'center', //居中对齐
                'right', //右对齐
                '|', //分割线
                'link', //超链接
                'unlink' //清除链接
            ]
        });
        //监听提交
        form.on('submit(submitBut)', function (data) {
            if (layedit.getText(index)) {
                var data = {
                    action: "add",
                    title: data.field.title,
                    content: layedit.getContent(index)
                };
                $.ajax({
                    type: "post",
                    url: '/MessageServlet.do',
                    data: data,
                    dataType: "Json",
                    success: function (data) {
                        if (data.tip) {
                            yesMsg(data.msg);
                            location.href = 'lyagent_list.html';
                        } else {
                            noMsg(data.msg);
                        }
                    }
                })
                ;
            } else {
                noMsg('必选项不能为空');
            }
            return false;
        });

        $.ajax({
            type: "get",
            url: "/UserServlet.do",
            data: {action: "getUser"},
            dataType: "Json",
            success: function (data) {
                if (data.user) {
                    $("input[name=studentName]").val(data.user.name);
                    index = layedit.build('content', {
                        tool: [
                            'strong', //加粗 'italic' //斜体
                            'underline', //下划线
                            'del', //删除线
                            '|', //分割线
                            'left', //左对齐
                            'center', //居中对齐
                            'right', //右对齐
                            '|', //分割线
                            'link', //超链接
                            'unlink' //清除链接
                        ]
                    });
                }
            }
        });
    });
</script>
</body>

</html>